//
//  GFFoodLIstCollectionViewCell.m
//  GoodFood
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFFoodLIstCollectionViewCell.h"

@interface GFFoodLIstCollectionViewCell ()
@property (nonatomic, strong) UIImageView *foodImageView;
@property (nonatomic, strong) UILabel *foodNameLabel;
@property (nonatomic, strong) UILabel *likeLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIImageView *topImageView;
@end

@implementation GFFoodLIstCollectionViewCell
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self creatImageView];
        [self creatLabel];
    }
    return self;
}
#pragma mark - 布局
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    self.foodImageView.frame = CGRectMake(5, 5, WIDTH - 10, HEIGHT / 1.3);
    self.foodNameLabel.frame = CGRectMake(0, HEIGHT / 1.3 + 5, WIDTH, HEIGHT / 7);
    
    self.likeLabel.frame = CGRectMake(0, HEIGHT / 1.3 + 5 + HEIGHT / 7, WIDTH / 2, HEIGHT - HEIGHT / 1.3 - 5 - HEIGHT / 7);
    
    self.priceLabel.frame = CGRectMake(WIDTH / 2, HEIGHT / 1.3 + 5 + HEIGHT / 7, WIDTH / 2, HEIGHT - HEIGHT / 1.3 - 5 - HEIGHT / 7);
    
    self.topImageView.frame = CGRectMake(self.foodImageView.frame.size.width / 5 * 4 + 5, 5, self.foodImageView.frame.size.width / 5 , self.foodImageView.frame.size.width / 5);


    
}
#pragma mark - 创建imageView
- (void)creatImageView {
    self.foodImageView = [[UIImageView alloc] init];
    self.foodImageView.layer.masksToBounds = YES;
    self.foodImageView.layer.cornerRadius = 5;
    [self.contentView addSubview:self.foodImageView];
    
    self.topImageView = [[UIImageView alloc] init];
}
#pragma mark - 创建label
- (void)creatLabel {
    self.foodNameLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.foodNameLabel];
    self.foodNameLabel.font = [UIFont systemFontOfSize:13];
    self.foodNameLabel.textAlignment = NSTextAlignmentCenter;
    
    self.likeLabel = [[UILabel alloc] init];
    self.likeLabel.font = [UIFont systemFontOfSize:12];
    self.likeLabel.textColor = [UIColor lightGrayColor];
    self.likeLabel.textAlignment = NSTextAlignmentCenter;
//    [self.contentView addSubview:self.likeLabel];
    
    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.font = [UIFont systemFontOfSize:12];
    self.priceLabel.textColor = [UIColor lightGrayColor];
    self.priceLabel.textAlignment = NSTextAlignmentCenter;
//    [self.contentView addSubview:self.priceLabel];
}
#pragma mark - 价格以及喜欢人数的判断
- (void)judegePriceAndLike {
    if (0 == self.model.food_price) {
        [self.priceLabel removeFromSuperview];
        if (0 == self.model.want_it_total) {
            self.foodNameLabel.frame = CGRectMake(0, HEIGHT / 1.3 + 5, WIDTH, HEIGHT - HEIGHT / 1.3 - 5);
            [self.likeLabel removeFromSuperview];
        } else {
            self.likeLabel.frame = CGRectMake(WIDTH / 3, HEIGHT / 1.3 + 5 + HEIGHT / 7, WIDTH - WIDTH / 3 * 2, HEIGHT - HEIGHT / 1.3 - 5 - HEIGHT / 7);
            
            [self.contentView addSubview:self.likeLabel];
        }
    } else {
        [self.contentView addSubview:self.priceLabel];
        if (0 == self.model.want_it_total) {
            self.priceLabel.frame = CGRectMake(WIDTH / 3, HEIGHT / 1.3 + 5 + HEIGHT / 7, WIDTH - WIDTH / 3 * 2, HEIGHT - HEIGHT / 1.3 - 5 - HEIGHT / 7);
            [self.likeLabel removeFromSuperview];
            
        } else {
            [self.contentView addSubview:self.likeLabel];

        }
    }
}
#pragma mark - set
- (void)setModel:(GFOtherFoodModel *)model {
    if (_model != model) {
        _model = model;
    }
    //图片
    [self.foodImageView setImageWithURL:[NSURL URLWithString:model.picture_url] placeholderImage:[UIImage imageNamed:@"category_1"]];
    //名称
    self.foodNameLabel.text = model.food_name;
    //根据价格和喜欢人数进行布局
    [self judegePriceAndLike];
    
    //喜欢
    NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor redColor]};
    NSString *like = [NSString stringWithFormat:@"%ld人喜欢", model.want_it_total];
    NSMutableAttributedString *attributeLike = [NSMutableAttributedString attributedWithString:like withRangeLocation:0 withRangeLength:like.length - 3 withDic:dic];
    self.likeLabel.attributedText = attributeLike;
    //价格
    NSString *price = [NSString stringWithFormat:@"%ld元", model.food_price];
    NSMutableAttributedString *attributePrice = [NSMutableAttributedString attributedWithString:price withRangeLocation:0 withRangeLength:price.length - 1 withDic:dic];
    self.priceLabel.attributedText = attributePrice;
    //top图片排名
    [self.topImageView removeFromSuperview];
    for (int i = 0; i < self.arraySort.count; i ++) {
        if ([model.food_name isEqualToString:[self.arraySort[i] objectForKey:@"food_name"]]) {
            [self.contentView addSubview:self.topImageView];
            self.topImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"top%d", i + 1]];
        }
    }

    
}
@end
